package cn.wzy.service.impl;

import cn.wzy.mapper.CarResourcesMapper;
import cn.wzy.pojo.CarResources;
import cn.wzy.results.ResponseResult;
import cn.wzy.service.CarResourcesService;
import cn.wzy.vo.CarResourcesDTO;
import cn.wzy.vo.GoPageVo;
import cn.wzy.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * (CarResources)表服务实现类
 *
 * @author makejava
 * @since 2023-01-24 10:49:10
 */
@Service
public class CarResourcesServiceImpl extends ServiceImpl<CarResourcesMapper, CarResources> implements CarResourcesService {

 public ResponseResult getCarResourcesPage(GoPageVo goPageVo) {
        Page<CarResources> page = new Page<>(goPageVo.getPageIndex(),goPageVo.getPageSize());
        LambdaQueryWrapper<CarResources> queryWrapper = new LambdaQueryWrapper<>();
        if (goPageVo.getKeyword()!=null && !"".equals(goPageVo.getKeyword())){
            queryWrapper.like(CarResources::getCarId,goPageVo.getKeyword());
        }
        List<CarResources> productTypes = page(page, queryWrapper).getRecords();
        PageVo pageVo = new PageVo(productTypes, page.getTotal(), goPageVo.getPageSize());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult saveCarResources(CarResources carResources) {
        //        判断是否id存在，操作类型
        if (Objects.isNull(carResources.getId())){
//            添加操作
            save(carResources);
            return ResponseResult.okResult();
        }else {
//            修改操作
            updateById(carResources);
            return ResponseResult.okResult();
        }
    }

    @Override
    public ResponseResult deleteCarResources(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCarResourcesList(List<Long> ids) {
       removeByIds(ids);
       return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCarResourcesByCarId(Long carId) {

     LambdaQueryWrapper<CarResources> queryWrapper = new LambdaQueryWrapper<>();
     queryWrapper.eq(CarResources::getCarId,carId);
     List<CarResources> carResourcesList = list(queryWrapper);
     return ResponseResult.okResult(carResourcesList);
    }

    @Transactional
    @Override
    public ResponseResult saveCarResourcesAll(CarResourcesDTO carResourcesDTO) {
        LambdaQueryWrapper<CarResources> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarResources::getCarId,carResourcesDTO.getCarId());
        remove(queryWrapper);
        List<CarResources> carResources = carResourcesDTO.getImgList();
        for (CarResources carResource : carResources) {
            carResource.setCarId(carResourcesDTO.getCarId());
        }
        saveBatch(carResources);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCarResources(Long id) {
       CarResources carResources = getById(id);
       return ResponseResult.okResult(carResources);
    }
}

